﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using ChinaSoft.DBUtility;//Please add references

namespace ChinaSoft.DAL.Common
{
    public class Pager
    {
        /// <summary>
        /// 根据要查询的sql语句查询出总共有多少条记录
        /// 作者：卢亚东
        /// 时间：2011-07-11
        /// </summary>
        /// <param name="strWhere">SQL查询语句</param>
        /// <returns></returns>
        public int RecordCount(string tableName, string strWhere)
        {
            object result = DbHelperOra.GetSingle("select count(*) from " + tableName + " where 1=1 " + strWhere);
            try
            {
                return Convert.ToInt32(result);
            }
            catch { }
            return 0;
        }

        /// <summary>
        /// 获取分页数据
        /// 作者：卢亚东
        /// 修改时间：2011-7-14
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="strKey">主键名</param>
        /// <param name="PageSize">每页的个数（默认是10个）</param>
        /// <param name="PageIndex">第几页</param>
        /// <param name="strFields">要查询的字段（默认是*）</param>
        /// <param name="strWhere">查询的条件（不带where,以and 开头的查询条件）</param>
        /// <param name="strOrder">排序的条件（不带order by）</param>
        /// <param name="strGroupBy">分组的条件(不带group by)</param>
        /// <returns></returns>
        public DataSet GetList(string tableName, string strKey, int PageSize, int PageIndex, string strFields, string strWhere, string strOrder, string strGroupBy)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Tables", SqlDbType.VarChar, 1000),
					new SqlParameter("@PrimaryKey", SqlDbType.VarChar, 100),
					new SqlParameter("@Sort", SqlDbType.VarChar,200),
					new SqlParameter("@CurrentPage", SqlDbType.Int),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@Fields", SqlDbType.VarChar,1000),
					new SqlParameter("@Filter", SqlDbType.VarChar,1000),
					new SqlParameter("@Group", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = tableName;
            parameters[1].Value = strKey;
            parameters[2].Value = strOrder;
            parameters[3].Value = PageIndex;
            parameters[4].Value = PageSize;
            parameters[5].Value = strFields;
            parameters[6].Value = strWhere;
            parameters[7].Value = strGroupBy;
            return DbHelperOra.RunProcedure("SqlPagination", parameters, tableName);
        }
    }
}
